
import UIKit
//遵守委托协议
class ChecklistViewController: UITableViewController,AddItemViewControllerDelegate {
    var checklist: Checklist!

    override func viewDidLoad() {
        super.viewDidLoad()
         title = checklist.name
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
//    设置 项数
    override func tableView(_ tableView: UITableView,
                            numberOfRowsInSection section: Int) -> Int {
        return checklist.items.count
    }
//    渲染cell
    override func tableView(_ tableView: UITableView,
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //获取组件
        let cell = tableView.dequeueReusableCell(
            withIdentifier: "checklistItem", for: indexPath)
//        获取指定项
        let item = checklist.items[indexPath.row]
//        设置文本及选择
        configureText(for: cell, with: item)
        configureCheckmark(for: cell, with: item)
        return cell
    }
//     点击切换选择
    override func tableView(_ tableView: UITableView,
                            didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            let item = checklist.items[indexPath.row]
            item.toggleChecked()
            configureCheckmark(for: cell, with: item)
        }
        tableView.deselectRow(at: indexPath, animated: true)
//        saveChecklistItems()
    }
//    左划 删除
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//        数组移除指定行
        checklist.items.remove(at: indexPath.row)
        let indexPaths = [indexPath]
//        组件删除行
        tableView.deleteRows(at: indexPaths, with: .automatic)
//        saveChecklistItems()
    }
    func configureCheckmark(for cell: UITableViewCell,with item: ChecklistItem) {
        let label = cell.viewWithTag(2) as! UILabel
        label.textColor = view.tintColor
        if item.checked {
            label.text = "√"
        } else {
            label.text = ""
        }
    }
    func configureText(for cell: UITableViewCell,
                       with item: ChecklistItem) {
        let label = cell.viewWithTag(1) as! UILabel
//        label.text = item.text
        label.text = "\(item.itemID):\(item.text)"
    }

//   告诉对象B，对象A现在是你的委托了。
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //指定转场
        if segue.identifier == "AddItem" {
            let navigationController = segue.destination as! UINavigationController
            let controller = navigationController.topViewController as! AddItemTableViewController
            controller.delegate = self
        } else if segue.identifier == "EditItem" {
            let navigationController = segue.destination as! UINavigationController
            let controller = navigationController.topViewController as! AddItemTableViewController
            controller.delegate = self
            if let indexPath = tableView.indexPath(for: sender as! UITableViewCell) {
                controller.itemToEdit = checklist.items[indexPath.row]
            }
        }
    }
    //委托方法cancel
    func addItemControllerDidCancel(_ controller: AddItemTableViewController) {
        dismiss(animated: true, completion: nil)
    }
    //委托方法addItem
    func addItemController(_ controller: AddItemTableViewController, didFinishAdding item: ChecklistItem) {
        let newRowIndex = checklist.items.count
        checklist.items.append(item)
        
        let indexPath = IndexPath(row: newRowIndex, section: 0)
        let indexPaths = [indexPath]
        tableView.insertRows(at: indexPaths, with: .automatic)
        
        dismiss(animated: true, completion: nil)
//        saveChecklistItems()
    }
    //修改的done
    func addItemController(_ controller: AddItemTableViewController, didFinishEditing item: ChecklistItem) {
//        ChecklistItem对象的行号。行号和ChecklistItem在items数组中的索引值是一致的，然后你通过index(of)方法来返回这个index。
        if let index = checklist.items.index(of: item){
            let indexPath = IndexPath(row: index, section: 0)
//            找到指定键值对
            if let cell = tableView.cellForRow(at: indexPath) {
                configureText(for: cell, with: item)
            }
        }
        dismiss(animated: true, completion: nil)
//        saveChecklistItems()
    }

}

